calHist() 您所在的位置:网站首页 calchist opencv calHist()

calHist()

#calHist()| 来源: 网络整理| 查看: 265

calHist() - Calculate histogram using openCV and C++

In practically every element of computer vision, histograms are used. For threshold, we employ gray-scale histograms. For white balance, we employ histograms. For object tracking in photos, such as with the CamShift technique, we use colour histograms. Color histograms are used as features, and colour histograms in several dimensions are included.

In a more abstract sense, they form the HOG and SIFT descriptors from histograms of visual gradients.

A histogram is also a bag-of-visual-words representation, which is widely employed in image search engines and machine learning. And, more than likely, this isn’t the first time you’ve seen histograms in your studies.

So, why do histograms come in handy?

Because histograms depict a set of data frequency distribution. And it turns out that looking at these frequency distributions is a dominant method to develop simple image processing techniques... as well as really powerful machine learning algorithms.

This blog post will summarize image histograms, as well as how to calculate colour histograms from video using openCV and C++.

What is Histogram ?

You might think of a histogram as a graph or plot that shows how an image’s intensity distribution is distributed. It’s a graph with pixel values (usually ranging from 0 to 255) on the X-axis and the number of pixels in the picture on the Y-axis.

It’s just a different way of looking at the image. When you look at the histogram of an image, you may get a sense of the image’s contrast, brightness, intensity distribution, and so on.

Almost all image processing software today includes a histogram feature.

Gray Scale Image Histogram

calHist() function in openCV

cv.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

So now we use calcHist() function to find the histogram. Let’s familiarize with the function and its parameters :

images : this is the uint8 or float32 source image.

“[img]” should be written in square brackets.

channels: It is the channel index for which the histogram is calculated.

If the input is a gray-scale image, the value is [0].

To calculate the histogram of the blue, green, or red channel in a colour image, pass [0], [1], or [2].

mask: It is given as “None” to find the histogram of the entire image.

But if you want to find histogram of a particular region of image, create a mask image for that and give it as a mask.

histSize: Our BIN count is represented by histSize. Must be enclosed in square brackets. We pass [256] for full scale.

range: It’s usually [0,256].

Code #include "opencv2/highgui.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc.hpp" #include const int histSize = 256; void drawHistogram(cv::Mat& b_hist,cv::Mat& g_hist,cv::Mat& r_hist) { int hist_w = 512; int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); cv::Mat histImage(hist_h, hist_w, CV_8UC3, cv::Scalar(0, 0, 0)); cv::normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat()); cv::normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat()); cv::normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat()); for (int i = 1; i < histSize; i++) { cv::line( histImage, cv::Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at(i - 1))), cv::Point(bin_w * (i), hist_h - cvRound(b_hist.at(i))), cv::Scalar(255, 0, 0), 2, 8, 0); cv::line( histImage, cv::Point(bin_w * (i - 1), hist_h - cvRound(g_hist.at(i - 1))), cv::Point(bin_w * (i), hist_h - cvRound(g_hist.at(i))), cv::Scalar(0, 255, 0), 2, 8, 0); cv::line( histImage, cv::Point(bin_w * (i - 1), hist_h - cvRound(r_hist.at(i - 1))), cv::Point(bin_w * (i), hist_h - cvRound(r_hist.at(i))), cv::Scalar(0, 0, 255), 2, 8, 0); } cv::namedWindow("calcHist Demo", cv::WINDOW_AUTOSIZE); cv::imshow("calcHist Demo", histImage); } int main(int argc, char **argv) { cv::Mat src, dst; cv::VideoCapture cap; if (argc != 2) cap.open(0); else cap.open(argv[1]); if (!cap.isOpened()) { std::cerr


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有